/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.firebird.base.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * @author qiuxj
 * @date 2009-11-14
 */
public class Page implements Serializable {

	private static final long serialVersionUID = 9030682628072361206L;
	public static final int DEFAULT_PAGE_SIZE = 15;
	private List result;
	private int start;
	private int pageSize;
	private int totalCount;

	public Page() {
		this(new ArrayList(), 0, 0, 15);
	}

	public Page(List results, int start, int totalCount, int pageSize) {
		this.pageSize = 15;

		if (results == null) {
			this.result = new ArrayList();
		} else if (results.size() <= pageSize) {
			this.result = results;
		} else {
			this.result = new ArrayList();
			for (int i = start; (i < start + pageSize) && (i < results.size()); ++i)
				this.result.add(results.get(i));
		}
		this.start = start;
		this.pageSize = ((pageSize > 0) ? pageSize : 15);
		this.totalCount = totalCount;
	}

	public int getCurrentPageNo() {
		return (this.start / this.pageSize + 1);
	}

	public int getLastPageNo() {
		return ((this.totalCount + this.pageSize - 1) / this.pageSize);
	}

	public List getResult() {
		return this.result;
	}

	public int getTotalCount() {
		return this.totalCount;
	}

	public boolean hasNextPage() {
		return (getCurrentPageNo() < getLastPageNo());
	}

	public boolean hasPrevPage() {
		return (getCurrentPageNo() > 1);
	}

	public static int getStartOfPage(int pageNo) {
		return getStartOfPage(pageNo, 15);
	}

	public static int getStartOfPage(int pageNo, int pageSize) {
		return Math.max((pageNo - 1) * pageSize, 0);
	}

	public String toString() {
		return new ToStringBuilder(this).append("totalCount", getTotalCount())
				.append("currentPageNo", getCurrentPageNo()).append(
						"lastPageNo", getLastPageNo()).append("result",
						getResult()).toString();
	}

}
